<?php

namespace app\tiktok\logic;
use think\facade\Db;
use DateTime;
use app\tiktok\logic\OrderLogic;
class CooperateLogic extends BasicLogic
{


    /**
     * @desc    达人合作看板接口
     * @date    2025-07-31
     * @author  luckyXiong
     */
    public function getCooperateList($params){
        $shop_id              = $params['shop_id']??'17';
        $cooperate_time       = $params['cooperate_time']??'';
        $fb_admin_id          = $params['fb_admin_id']??'';
        $customDate           = $params['customDate']??'';
        $where                = [];
        $where[]              = ['shop_id','=',$shop_id];
        if($fb_admin_id){
            $where[]          = ['fb_admin_id','=',$fb_admin_id];
        }


        if($customDate==1){
            $lastWeekStart =  date('Y-m-d H:i:s', strtotime('-1 week'));
            $lastWeekEnd   =  date('Y-m-d 00:00:00',time());
            $where[]       = ['cooperate_time','between', [$lastWeekStart, $lastWeekEnd]];
        }elseif($customDate==2){
            $cooperate_start      = date('Y-m-d 00:00:00',strtotime(date("Y-m-01")));
            $cooperate_end        = date('Y-m-d 00:00:00',time());
            $where[]              = ['cooperate_time','between', [$cooperate_start, $cooperate_end]];
        }

        if($cooperate_time){
            $where[]  = ['cooperate_time', '>=', date('Y-m-d H:i:s',strtotime($cooperate_time[0]))];
            $where[]  = ['cooperate_time', '<=', date('Y-m-d H:i:s',strtotime($cooperate_time[1]) + 3600*24-1)];
        }

        $list  = [];

        //总合作素材数量;
        $all_cooperate_nums         = Db::name('tik_kol_cooperate')->where('shop_id',$shop_id)->count();
        $list['all_cooperate_nums'] = $all_cooperate_nums;


        //合作素材条数
        $time_cooperate_nums  = Db::name('tik_kol_cooperate')
                              ->where($where)
                              ->count();

        $list['time_cooperate_nums'] = $time_cooperate_nums;




        //素材回收进度
        $video_cooperate_nums    = Db::name('tik_kol_cooperate')
            ->where($where)
            ->where('fulfillment_status','3')
            ->count();
        $month_cooperate_sums    = 60;
        $month_cooperate_rate    = getRes(($video_cooperate_nums/$month_cooperate_sums)*100);
        $month_cooperate_rate    = $month_cooperate_rate.'%';

        $list['video_cooperate_nums_arr'] = [
            'video_cooperate_nums'=>$video_cooperate_nums,
            'month_cooperate_sums'=>$month_cooperate_sums,
            'month_cooperate_rate'=>$month_cooperate_rate,
        ];


        //本月合作人数
        $month_cooperate_persons = Db::name('tik_kol_cooperate')
            ->where($where)
            ->group('expert_name')
            ->count();
        $list['cooperate_persons_nums'] = $month_cooperate_persons;




        //未发布超时达人;
        $out_persons             = Db::name('tik_kol_cooperate')
            ->where('shop_id',$shop_id)
            ->where('is_out',2)
            ->whereNotNull('publish_time')
            ->count();

        $list['out_persons']   = $out_persons;


        //合作方式比例;
        $cooperate_type_pay = Db::name('tik_kol_cooperate')
            ->where(['cooperate_type'=>2])
            ->count();

        $cooperate_type_10 = Db::name('tik_kol_cooperate')
            ->where(['cooperate_type'=>3])
            ->count();

        $cooperate_rate = getResFour($cooperate_type_pay/($cooperate_type_pay+$cooperate_type_10))*100;
        $cooperate_rate = $cooperate_rate.'%';

        $cooperate_rate_10 = getResFour($cooperate_type_10/($cooperate_type_pay+$cooperate_type_10))*100;
        $cooperate_rate_10 = $cooperate_rate_10.'%';

        $list['cooperate_type_arr'] = [
            'cooperate_type_pay'  =>$cooperate_type_pay,
            'cooperate_type_10'   =>$cooperate_type_10,
            'cooperate_rate'      =>$cooperate_rate,
            'cooperate_rate_10'  =>$cooperate_rate_10,
        ];


        //合作达人物流情况
        $fulfillment_status_1 = Db::name('tik_kol_cooperate')
            ->where($where)
            ->where('fulfillment_status',1)
            ->count();

        $fulfillment_status_2 = Db::name('tik_kol_cooperate')
            ->where($where)
            ->where('fulfillment_status',2)
            ->count();

        $fulfillment_status_3 = Db::name('tik_kol_cooperate')
            ->where($where)
            ->where('fulfillment_status',3)
            ->count();



        $fulfillment_status_4 = Db::name('tik_kol_cooperate')
            ->where($where)
            ->where('fulfillment_status',4)
            ->count();


        $list['fulfillment_status_arr'] = [
            'fulfillment_status_1' =>$fulfillment_status_1,
            'fulfillment_status_2' =>$fulfillment_status_2,
            'fulfillment_status_3' =>$fulfillment_status_3,
            'fulfillment_status_4' =>$fulfillment_status_4,
        ];


        //各建联素材待回收条数;
        if($customDate==1){ // 一周的时候
            $sku_cooperate_pre =  Db::name('tik_kol_cooperate')
                ->field('id,spu,count(sku) as spu_count')
                ->where($where)
                ->where('cooperate_status',2)
                ->group('spu')
                ->select()
                ->toArray();
        }else{
            $sku_cooperate_pre =  Db::name('tik_kol_cooperate')
                ->field('id,spu,count(sku) as spu_count')
                ->where('shop_id',$shop_id)
                ->where('cooperate_status',2)
                ->group('spu')
                ->select()
                ->toArray();
        }

        $list['sku_cooperate_pre'] = $sku_cooperate_pre;


        //各建联素材已回收条数;
        if($customDate==1){ // 一周的时候
            $sku_cooperate_complete =  Db::name('tik_kol_cooperate')
                ->field('id,spu,count(sku) as spu_count')
                ->where($where)
                ->where('cooperate_status',3)
                ->group('spu')
                ->select()
                ->toArray();
        }else{
            $sku_cooperate_complete =  Db::name('tik_kol_cooperate')
                ->field('id,spu,count(sku) as spu_count')
                ->where('shop_id',$shop_id)
                ->where('cooperate_status',3)
                ->group('spu')
                ->select()
                ->toArray();
        }
        $list['sku_cooperate_complete'] = $sku_cooperate_complete;

        return  $list;


    }



    /**
     * @desc    达人个人看板接口
     * @date    2025-08-01
     * @author  luckyXiong
     */
    public function getPersonList($params){
       $expert_id  = $params['expert_id']??'';
       $field      = 'id,expert_id,expert_name,email,
                      tags,level,fb_admin_id,shop_id,platform_id,kol_phone,link';
       $info       = Db::name('tik_kol')->field($field)->where(['expert_id'=>$expert_id])->find();
       //获取达人的素材列表信息;
       $cooperate_info = Db::name('tik_kol_cooperate')
                     ->field('expert_id,storage_url,payment_price,spu')
                     ->where(['expert_id'=>$expert_id])
                     ->select()
                     ->toArray();
        $logic                  = new OrderLogic();
        $operater               = $logic->get_fb_name($info['fb_admin_id']);
        $shop_name              = $logic->get_shop_name($info['shop_id']);
        //合作价格
        $cooperate_price        = $logic->get_cooperate_price($info['expert_id']);
        $cooperate_price        = $cooperate_price ?  : '';
        $cooperate_nums         = 0;
        $cooperate_spu          = '';
        $map                    = [];
        $map[]                  = ['expert_id','=',$expert_id];
        $map[]                  = ['storage_url','<>',''];
        $video_list             = Db::name('tik_kol_video')
                                 ->field('id,storage_url,spu')
                                 ->where($map)
                                 ->select()
                                 ->toArray();

        $cooperate_spu          = '';
        $storage_urls           = '';
        if($video_list) {
            $spus           = array_column($video_list, 'spu');
            $storage_urls   = array_column($video_list, 'storage_url');
            $cooperate_spu  = $spus ? implode(',',$spus) : '';
        }

        if($cooperate_info) {
            $cooperate_nums = count($cooperate_info);
          }
         return  [
               'person_info'=>[
                   'id'         =>$info['id']??'',
                   'expert_id'  =>$info['expert_id']??'',
                   'expert_name'=>$info['expert_name']??'',
                   'email'      =>$info['email']??'',
                   'kol_phone'  =>$info['kol_phone']??'',
                   'link'       =>$info['link']??'',
                   'tags'       =>$info['tags']??'',
                   'level'      =>$info['level']??'',
                   'shop_id'     =>$info['shop_id']??'',
                   'platform_id' =>$info['platform_id']??'',
                   'operater'    =>$operater,
                   'head_url'    =>'https://cntiktok.oss-cn-hangzhou.aliyuncs.com/images/240525/1716606742.png',
                   'shop_name'        =>$shop_name,
                   'cooperate_price'  =>$cooperate_price,
                   'cooperate_nums'   =>$cooperate_nums,
                   'cooperate_spu'    =>$cooperate_spu,
               ],
               'video_info' =>$storage_urls??'',
           ];
    }



    /**
     * @desc    达人合作看板明细接口
     * @date    2025-07-31
     * @author  luckyXiong
     */
    public function getCooperateInfo($params){
        $shop_id              = $params['shop_id']??'17';
        $cooperate_time       = $params['cooperate_time']??'';
        $fb_admin_id          = $params['fb_admin_id']??'';
        $customDate           = $params['customDate']??'';
        $type                 = $params['type']??'';
        $spu                  = $params['spu']??'';
        $fulfillment_status   = $params['fulfillment_status']??'';
        $where                = [];
        $where[]              = ['shop_id','=',$shop_id];
        if($fb_admin_id){
            $where[]          = ['fb_admin_id','=',$fb_admin_id];
        }


        if($customDate==1){
            $lastWeekStart =  date('Y-m-d H:i:s', strtotime('-1 week'));
            $lastWeekEnd   =  date('Y-m-d 00:00:00',time());
            $where[]       = ['cooperate_time','between', [$lastWeekStart, $lastWeekEnd]];
        }elseif($customDate==2){
            $cooperate_start      = date('Y-m-d 00:00:00',strtotime(date("Y-m-01")));
            $cooperate_end        = date('Y-m-d 00:00:00',time());
            $where[]              = ['cooperate_time','between', [$cooperate_start, $cooperate_end]];
        }

        if($cooperate_time){
            $where[]  = ['cooperate_time', '>=', date('Y-m-d H:i:s',strtotime($cooperate_time[0]))];
            $where[]  = ['cooperate_time', '<=', date('Y-m-d H:i:s',strtotime($cooperate_time[1]) + 3600*24-1)];
        }

        if($fulfillment_status){
            $where[]  = ['fulfillment_status','=',$fulfillment_status];
        }

        $list  = [];


        switch($type){
            case 1:
                $list = Db::name('tik_kol_cooperate')->where('shop_id',$shop_id)->select()->toArray();
                break;
            case 2:
            case 7:
                $list = Db::name('tik_kol_cooperate')->where($where)->select()->toArray();
                break;
            case 3:
                $list = Db::name('tik_kol_cooperate')
                    ->where($where)
                    ->where('fulfillment_status','3')
                    ->select()->toArray();
                break;
            case 4:
                $list = Db::name('tik_kol_cooperate')
                    ->where($where)
                    ->group('expert_name')
                    ->select()->toArray();
                break;
            case 5:
                $list = Db::name('tik_kol_cooperate')
                    ->where('shop_id',$shop_id)
                    ->where('is_out',2)
                    ->whereNotNull('publish_time')
                    ->select()->toArray();
                break;
            case 6:
                $list = Db::name('tik_kol_cooperate')
                    ->whereIn('cooperate_type',[2,3,4])
                    ->select()->toArray();
                break;

            case 8:
                if($spu){
                    if($customDate==1){ // 一周的时候
                        $list =  Db::name('tik_kol_cooperate')
                            ->where($where)
                            ->where('cooperate_status',2)
                            ->where('spu',$spu)
                            ->select()
                            ->toArray();
                    }else{
                        $list =  Db::name('tik_kol_cooperate')
                            ->where('shop_id',$shop_id)
                            ->where('spu',$spu)
                            ->where('cooperate_status',2)
                            ->select()
                            ->toArray();
                    }
                }else{
                    if($customDate==1){ // 一周的时候
                        $list =  Db::name('tik_kol_cooperate')
                            ->where($where)
                            ->where('cooperate_status',2)
                            ->group('spu')
                            ->select()
                            ->toArray();
                    }else{
                        $list =  Db::name('tik_kol_cooperate')
                            ->where('shop_id',$shop_id)
                            ->where('cooperate_status',2)
                            ->select()
                            ->toArray();
                    }
                }
                break;
            case 9:
                if($spu){
                    if($customDate==1){ // 一周的时候
                        $list =  Db::name('tik_kol_cooperate')
                            ->where($where)
                            ->where('cooperate_status',3)
                            ->where('spu',$spu)
                            ->select()
                            ->toArray();
                    }else{
                        $list =  Db::name('tik_kol_cooperate')
                            ->where('shop_id',$shop_id)
                            ->where('cooperate_status',3)
                            ->where('spu',$spu)
                            ->select()
                            ->toArray();
                    }
                }else{
                    if($customDate==1){ // 一周的时候
                        $list =  Db::name('tik_kol_cooperate')
                            ->where($where)
                            ->where('cooperate_status',3)
                            ->group('spu')
                            ->select()
                            ->toArray();
                    }else{
                        $list =  Db::name('tik_kol_cooperate')
                            ->where('shop_id',$shop_id)
                            ->where('cooperate_status',3)
                            ->group('spu')
                            ->select()
                            ->toArray();
                    }
                }

                break;
            default :
                return  '';
        }

        $arr_list = [];

        //批量获取创建人;
        $operater_ids    = array_column($list,'fb_admin_id');
        $admin_users     = Db::name('system_admin')->field('id,username')->whereIn('id',$operater_ids)->select()->toArray();
        $admin_users_map = array_column($admin_users,null,'id');
        //批量获取联系方式
        $expert_ids      = array_column($list,'expert_id');
        $admin_users     = Db::name('tik_kol')->field('id,expert_id,email')->whereIn('expert_id',$expert_ids)->select()->toArray();
        $email_map       = array_column($admin_users,null,'expert_id');

        foreach($list as $k=>$v){
            $admin_users_info               = $admin_users_map[$v['fb_admin_id']]??'';
            $email_map_info                 = $email_map[$v['expert_id']]??'';
            $arr_list[$k]['expert_id']      = $v['expert_id']??'';
            $arr_list[$k]['expert_name']    = $v['expert_name']??'';
            $arr_list[$k]['link']           = $v['link']??'';
            $arr_list[$k]['cooperate_time'] = $v['cooperate_time']??'';
            $arr_list[$k]['operater']       = $admin_users_info['username']??'';
            $arr_list[$k]['platform_id']    = $v['platform_id']??'';
            $arr_list[$k]['email']          = $email_map_info['email']??'';
            $arr_list[$k]['cooperate_type'] = $v['cooperate_type']??'';
            $arr_list[$k]['cooperate_status']= $v['cooperate_status']??'';
            $arr_list[$k]['spu']            = $v['spu']??'';
            $arr_list[$k]['sku']            = $v['sku']??'';
            $arr_list[$k]['publish_url']    = $v['publish_url']??'';
            $arr_list[$k]['fulfillment_status']    = $v['fulfillment_status']??'';
            $arr_list[$k]['expire_time']        = $v['expire_time']??'';
            $arr_list[$k]['remarks']            = $v['remarks']??'';
            $arr_list[$k]['is_out']             = $v['is_out']??'';
        }
        return  $arr_list;
    }










}